function Snake(){
    //蛇的初始化身体
   this.body = [
       {'row':3, "col": 5},
       {'row':3, "col": 4},
       {'row':3, "col": 3},
       {'row':3, "col": 2},
   ];
//信号量，设置的运动方向
this.direction = 'R';
this.willDirection = 'R'
}
//蛇的运动
Snake.prototype.update = function(){
//当前的direction接收一下willdirection
this.direction = this.willDirection;
switch(this.direction) {
    case 'R':
        this.body.unshift({'row': this.body[0].row, 'col':this.body[0].col + 1})
        break;
    case "D":
        this.body.unshift({'row': this.body[0].row + 1, 'col':this.body[0].col})
        break;
    case 'L':
        this.body.unshift({'row': this.body[0].row, 'col':this.body[0].col - 1});
        break;
    case 'U':
        this.body.unshift({'row': this.body[0].row - 1, 'col':this.body[0].col});
        break;
}
//死亡的判断
if(this.body[0].col >game.col - 1 || this.body[0].row > game.row - 1 || this.body[0].col<0 ||this.body[0].row < 0) {
    alert('您当前的得分为' + game.score + "分")
    this.body.shift();
    clearInterval(game.timer);
}
//自己撞到了自己的时候也会判定死亡
for(var i = 1; i < this.body.length; i++) {
    if(this.body[0].col ==this.body[i].col && this.body[0].row == this.body[i].row){
        alert('您当前的得分为' + game.score + "分")
        this.body.shift();
        clearInterval(game.timer);
    }

}
     if(this.body[0].row == game.food.row && this.body[0].col == game.food.col){

       game.food = new Food(game);
        //加分数
        game.score++;
       //让帧编号归0，因为蛇会蹿一下
       game.f = 0;
     } else {

        this.body.pop();
     }
};
Snake.prototype.changeDirection = function(d) {
    this.willDirection = d;
}
Snake.prototype.render = function(){

game.setColor(this.body[0].row,this.body[0].col,'-webkit-radial-gradient(center center,pink,red');
for (var i=1; i< this.body.length; i++){
    game.setColor(this.body[i].row, this.body[i].col, '-webkit-radial-gradient(center center,orange,red');
}
}

